using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;

namespace GameTank
{
    /// <summary>
    /// This is the main type for your game
    /// </summary>
    public class Game1 : Microsoft.Xna.Framework.Game
    {
        public static bool exit = false;
        public static ContentManager Content1;
        GraphicsDeviceManager graphics;
        SpriteBatch spriteBatch;
        public static Rectangle windowsBounder = new Rectangle(0, 0, 1000, 32 * 22 + 1); // vien ngoai man hinh

        public static Rectangle battleBounder = new Rectangle(0, 0, 24 * 32, 22 * 32); // duong vien bao quanh  battle
        public static MyGamePad gamepad = new MyGamePad();   // lop nay nam giua ban phim vay ly va dieu khien that game
        public static AudioLibrary AudioLib = new AudioLibrary(); // nhac va am thanh
        public static GamePlay gamePlay;

        protected Texture2D helpBackgroundTexture;
        protected Texture2D startBackgroundTexture, startElementsTexture;
        protected Texture2D warningBackgroundTexture;

        FormContent MenuList = new FormContent();
        List<InvisibleGameEntity> _invisibleGameEntity = new List<InvisibleGameEntity>();

        private SpriteFont endGameFont, smallFont, largeFont, normalFont;
        public Game1()
        {
            graphics = new GraphicsDeviceManager(this);
            Content.RootDirectory = "Content";
            graphics.PreferredBackBufferWidth = windowsBounder.Width;
            graphics.PreferredBackBufferHeight = windowsBounder.Height;
        }

        /// <summary>
        /// Allows the game to perform any initialization it needs to before starting to run.
        /// This is where it can query for any required services and load any non-graphic
        /// related content.  Calling base.Initialize will enumerate through any components
        /// and initialize them as well.
        /// 
        /// </summary>
        protected override void Initialize()
        {
            // TODO: Add your initialization logic here
            gamepad = new MyGamePad();
            base.Initialize();
        }

        /// <summary>
        /// LoadContent will be called once per game and is the place to load
        /// all of your content.
        /// </summary>
        protected override void LoadContent()
        {
            // Create a new SpriteBatch, which can be used to draw textures.
            
            
            // Load Font
            Game1.Content1 = Content;
            spriteBatch = new SpriteBatch(GraphicsDevice);
            endGameFont = Content.Load<SpriteFont>(@"SpriteFont2");
            normalFont = Content.Load<SpriteFont>("SpriteFont1");
            smallFont = Content.Load<SpriteFont>("menuSmall");
            largeFont = Content.Load<SpriteFont>("menuLarge");
            // adudio load content

            AudioLib.LoadContent(Content);
            _invisibleGameEntity.Add(gamepad);

            ModelDTO md = new ModelDTO();

            //md.FilePath = @"Construction\brick";
            //md.NFrame = 1;
            //md.NDirection = 1;
            //md.SaveToFile("brick.mdd");
            //
            //md.FilePath = @"Construction\stone";
            //md.NFrame = 1;
            //md.NDirection = 1;
            //md.SaveToFile("stone.mdd");
            //
            //helpBackgroundTexture = Content.Load<Texture2D>("helpForeground");
            //helpScene = new HelpScene(this, helpBackgroundTexture);
            //_gameScenes.Add(helpScene);
            //// create vitory
            //helpBackgroundTexture = Content.Load<Texture2D>("victoryCampain");
            //victoryScnene = new HelpScene(this, helpBackgroundTexture);
            //_gameScenes.Add(victoryScnene);

            // Create the Start Scene
           
            Texture2D startBackgroundTexture = Content.Load<Texture2D>("startbackground");
            FormContent.startMenu = new StartMenu(smallFont, largeFont,  startBackgroundTexture);                     
            gamePlay = new GamePlay();
            FormContent.gamePlay = gamePlay;
            startBackgroundTexture = Content.Load<Texture2D>("MenuScene");
            FormContent.gamPlayMenu = new GamePlayMenu(smallFont, largeFont, startBackgroundTexture);
            FormContent.quitMenu = new QuitMenu(smallFont, largeFont, startBackgroundTexture);
            startBackgroundTexture = Content.Load<Texture2D>("ReportBackground");
            FormContent.endMission = new EndMissionMenu(smallFont, largeFont, startBackgroundTexture);
            MenuList.ReInit();
            FormContent.startMenu.Show();
            
        }

        /// <summary>
        /// UnloadContent will be called once per game and is the place to unload
        /// all content.
        /// </summary>
        protected override void UnloadContent()
        {
            // TODO: Unload any non ContentManager content here
        }

        /// <summary>
        /// Allows the game to run logic such as updating the world,
        /// checking for collisions, gathering input, and playing audio.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        protected override void Update(GameTime gameTime)
        {
            // Allows the game to exit
            if (exit)
                Exit();
            if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
                this.Exit();

            foreach (var inv in _invisibleGameEntity)
            {
                inv.Update(gameTime);
            }

            MenuList.Update(gameTime);
            base.Update(gameTime);
        }

        /// <summary>
        /// This is called when the game should draw itself.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        protected override void Draw(GameTime gameTime)
        {
            GraphicsDevice.Clear(Color.CornflowerBlue);
            // TODO: Add your drawing code here

            MenuList.Draw(gameTime, spriteBatch);
            base.Draw(gameTime);
        }
    }
}
